package com.yonyou.crm.bpub.detail.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.detail.entity.*;
import com.yonyou.crm.sprc.lead.entity.LeadVO;
import com.yonyou.crm.sprc.lead.service.LeadBizServiceImpl;
import com.yonyou.iuap.context.InvocationInfoProxy;


@Service
public class LeadDetails extends BaseDetails {

	@Autowired
	LeadBizServiceImpl service;
	
	@Override
	public <T extends Serializable> T getDetail(long id) {
		LeadVO obj = service.getDetail(id);
		if ((obj != null) && (obj.getId() == null)) {
			obj = null;
		}
		return (T)obj;
	}
	
	/**
	 * 获取详情模板
	 */
	@Override
	public  <T extends Serializable> Map<String, List<DetailsGroupVO>> getLayout(T obj) {
		LeadVO vo = (LeadVO) obj;
		//TODO定义biztype
		//Long biztypeId = vo.getBiztypeId();
		long biztypeId = 11;
		return layoutService.getMobileDetailTemplateForCurrentUser(biztypeId);
	}
	
	@Override
	public <T extends Serializable> List<DetailsGroupVO> getHeader(T obj, Map<String, List<DetailsGroupVO>> layoutList) {
		List<DetailsGroupVO> headerlist = new ArrayList<>();
		
		LeadVO vo = (LeadVO)obj;
		DetailsGroupVO header = new DetailsGroupVO();
		List<DetailsItemVO> headerData = new ArrayList<>();
		DetailsItemVO item;
		String displayValue;
		
		item = new DetailsItemVO();
		item.setItemkey("name");
		item.setName("姓名");
		item.setValue(vo.getName());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("companyName");
		item.setName("公司名称");
		item.setValue(vo.getCompanyName());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("state");
		item.setName("状态");
		item.setValue(vo.getStateName() == null ? "" : vo.getStateName());
		item.setType("enum");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("mobile");
		item.setName("手机");
		item.setValue(vo.getMobile() == null ? "" : vo.getMobile());
		item.setType("phone");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("tel");
		item.setName("办公电话");
		item.setValue(vo.getTel() == null ? "" : vo.getTel());
		item.setType("phone");
		headerData.add(item);
		
		header.setGroupdata(headerData);
		headerlist.add(header);
		header = new DetailsGroupVO();
		headerData = new ArrayList<>();
		
		item = new DetailsItemVO();
		item.setItemkey("name");
		item.setName("姓名");
		item.setValue(vo.getName());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("gender");
		item.setName("性别");
		item.setId((vo.getGender() == null) ? "" : vo.getGender().toString());
		item.setValue((vo.getGenderName() == null) ? "" : vo.getGenderName());
		item.setType("enum");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("mobile");
		item.setName("手机");
		item.setValue(vo.getMobile() == null ? "" : vo.getMobile());
		item.setType("phone");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("source");
		item.setName("来源");
		item.setId((vo.getSource() == null) ? "" : vo.getSource().toString());
		item.setValue((vo.getSourceName() == null) ? "" : vo.getSourceName());
		item.setType("enum");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("industry");
		item.setName("行业");
		item.setId((vo.getIndustryId() == null) ? "" : vo.getIndustryId().toString());
		item.setValue((vo.getIndustryName() == null) ? "" : vo.getIndustryName());
		item.setType("enum");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("level");
		item.setName("线索等级");
		item.setId((vo.getLevel() == null) ? "" : vo.getLevel().toString());
		item.setValue((vo.getLevelName() == null) ? "" : vo.getLevelName());
		item.setType("enum");
		headerData.add(item);
		
		header.setGroupdata(headerData);
		headerlist.add(header);
		header = new DetailsGroupVO();
		headerData = new ArrayList<>();
		
		item = new DetailsItemVO();
		item.setItemkey("tel");
		item.setName("办公电话");
		item.setValue(vo.getTel() == null ? "" : vo.getTel());
		item.setType("phone");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("email");
		item.setName("邮箱");
		item.setValue(vo.getEmail() == null ? "" : vo.getEmail());
		item.setType("email");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("website");
		item.setName("网址");
		item.setValue(vo.getWebsite() == null ? "" : vo.getWebsite());
		item.setType("link");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("post");
		item.setName("职位");
		item.setValue(vo.getPostName() == null ? "" : String.valueOf(vo.getPostName()));
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("province_city_district");
		item.setName("省市区");
		String province = vo.getProvinceName() == null ? "" : vo.getProvinceName();
		String city = vo.getCityName() == null ? "" : vo.getCityName();
		String district = vo.getDistrictName() == null ? "" : vo.getDistrictName();
		String address = province + city + district;
		item.setValue(address);
		item.setType("address_pcd");
		headerData.add(item);
		
		header.setGroupdata(headerData);
		headerlist.add(header);
		header = new DetailsGroupVO();
		headerData = new ArrayList<>();
		
		item = new DetailsItemVO();
		item.setItemkey("ownerUserId");
		item.setName("负责人");
		displayValue = (vo.getOwnerUserInfo() == null || vo.getOwnerUserInfo().getName() == null) ? "" : vo.getOwnerUserInfo().getName();
		item.setValue(displayValue);
		item.setType("refertype");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("ownerUserDept");
		item.setName("部门");
		displayValue = (vo.getOwnerUserInfo() == null || vo.getOwnerUserInfo().getDeptName() == null) ? "" : vo.getOwnerUserInfo().getDeptName();
		item.setValue(displayValue);
		item.setType("refertype");
		headerData.add(item);
		
		header.setGroupdata(headerData);
		headerlist.add(header);
		header = new DetailsGroupVO();
		headerData = new ArrayList<>();

		item = new DetailsItemVO();
		item.setItemkey("remarks");
		item.setName("备注");
		item.setValue((vo.getRemarks() == null) ? "" : vo.getRemarks());
		item.setType("largetext");
		headerData.add(item);
		
		header.setGroupdata(headerData);
		headerlist.add(header);
		
		return headerlist;
	}

	@Override
	public <T extends Serializable> List<DetailsGroupVO> getBody(T obj, Map<String, List<DetailsGroupVO>> layoutList){
		List<DetailsGroupVO> body = new ArrayList<>();
		return body;
	}
	
	@Override
	public <T extends Serializable> List<DetailsFlagVO> getFlag(T obj){
		List<DetailsFlagVO> flags = new ArrayList<>();
		LeadVO vo = (LeadVO)obj;
		
		DetailsFlagVO flag = new DetailsFlagVO();
		String flagValue = (vo.getFollowFlag().equals("1")) ? "Y" : "N";
		flag.setDes("follow");
		flag.setValue(flagValue);
		flags.add(flag);
		
		return flags;
	}
	
	@Override
	public <T extends Serializable> List<String> getAction(T obj){
		List<String> actions = new ArrayList<>();
		LeadVO contactInfo = (LeadVO) obj;
		Long userId = new Long(InvocationInfoProxy.getParameter("id"));
		if (userId.toString().equals(contactInfo.getOwnerUserId().toString())) {
			actions.add("edit");
			actions.add("conversion"); //转化
			actions.add("close"); //关闭
		}
		return actions;
	}

}
